#include "history.h"
#include "ui_history.h"

History::History(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::History)
{
    ui->setupUi(this);
    timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &History::updateData);
    timer->start(1000);


    temperatureData.clear();
    humidityData.clear();
    lightData.clear();

    // 初始值设定（根据实际情况需要替换）
    temperature = 20.0; // 温度初始值
    humidity = 50.0; // 湿度初始值
    lightIntensity = 100.0; // 光照强度初始值
}

History::~History()
{
    delete ui;
}

void History::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);  // 开启抗锯齿

    // 绘制坐标轴
    drawAxes(&painter);

    // 绘制曲线
    drawCurve(&painter, temperatureData, Qt::red);  // 温度曲线（红色）
    drawCurve(&painter, humidityData, Qt::green);    // 湿度曲线（绿色）
    drawCurve(&painter, lightData, Qt::blue);      // 光照强度曲线（蓝色）
}

void History::updateData()
{
    static int x = 0;
    // 生成模拟数据（替换为你实际的数据）
        temperature = GlobalVal::temp;  // 模拟温度数据
        humidity = GlobalVal::hum;   // 模拟湿度数据
        lightIntensity = (GlobalVal::lht)/10;  // 模拟光照强度数据

        // 计算新的y值并添加到对应的数据列表中
        temperatureData.append(QPointF(x, temperature));
        humidityData.append(QPointF(x, humidity));
        lightData.append(QPointF(x, lightIntensity));

        // 保持数据量不超过窗口宽度
        if (temperatureData.size() > width()) {
            temperatureData.removeFirst();
            humidityData.removeFirst();
            lightData.removeFirst();
        }

        maxTime = x;
            minTime = x - width();
            maxValue = std::max(std::max(maxValue, temperature), std::max(humidity, lightIntensity));
               minValue = std::min(std::min(minValue, temperature), std::min(humidity, lightIntensity));
        // 更新x值
        ++x;

        // 刷新窗口
        update();
}

void History::drawAxes(QPainter *painter)
{
    // 定义亮橙色
    QColor brightOrange(255, 165, 0);

    QPen pen;
    pen.setColor(brightOrange);  // 设置画笔颜色为亮橙色
    pen.setWidth(2);
    painter->setPen(pen);

    int adjustedMinTime = std::max(static_cast<int>(minTime), 0);  // 确保 minTime 不小于 0
    int adjustedMaxTime = std::max(static_cast<int>(maxTime), 0);  // 如果 maxTime 是负数，则改为 0

    // 计算Y轴的最大值和最小值，确保间隔为10
    int yInterval = 10;  // Y轴间隔值
    if (minValue % yInterval != 0) {
        minValue = ((static_cast<int>(minValue) / yInterval) + 1) * yInterval;  // 向上取整到最接近的yInterval倍数
    }
    if (maxValue % yInterval != 0) {
        maxValue = (static_cast<int>(maxValue) / yInterval) * yInterval;  // 向下取整到最接近的yInterval倍数
    }

    // 绘制 X 轴
    int xAxisY = height() - margin;
    painter->drawLine(margin, xAxisY, width() - margin, xAxisY);  // 绘制 X 轴线
    for (int i = 0; i <= 10; ++i) {
        int xPos = margin + i * (width() - 2 * margin) / 10;
        painter->drawLine(xPos, xAxisY - 5, xPos, xAxisY + 5);  // 刻度

        // 设置文本颜色为亮橙色
        painter->setPen(brightOrange);
        int timeLabel = adjustedMinTime + i * (adjustedMaxTime - adjustedMinTime) / 10;
        painter->drawText(xPos - 10, xAxisY + 20, QString::number(timeLabel));
        painter->setPen(pen);  // 恢复原来的画笔设置
    }

    // 绘制 Y 轴
    int yAxisX = margin;
    painter->drawLine(yAxisX, margin, yAxisX, height() - margin);  // 绘制 Y 轴线
    int numYTicks = (maxValue - minValue) / yInterval;
    for (int i = 0; i <= numYTicks; ++i) {
        int yPos = height() - margin - i * (height() - 2 * margin) / numYTicks;
        painter->drawLine(yAxisX - 5, yPos, yAxisX + 5, yPos);  // 刻度

        // 设置文本颜色为亮橙色
        painter->setPen(brightOrange);
        int valueLabel = minValue + i * yInterval;
        painter->drawText(yAxisX - 30, yPos + 5, QString::number(valueLabel));
        painter->setPen(pen);  // 恢复原来的画笔设置
    }
}

void History::drawCurve(QPainter *painter, const QVector<QPointF>& data, const QColor& color)
{
    if (data.size() < 2) return;  // 至少需要两个点才能绘制曲线

    QPen pen;
    pen.setColor(color);
    pen.setWidth(2);
    painter->setPen(pen);

    // 防止 X 轴出现负轴
    int adjustedMinTime = std::max(static_cast<int>(minTime), 0);  // 确保 minTime 不小于 0
    int adjustedMaxTime = std::max(static_cast<int>(maxTime), 0);  // 如果 maxTime 是负数，则改为 0

    for (int i = 1; i < data.size(); ++i) {
        // 将数据映射到画布的坐标系中
        int x1 = margin + (data[i - 1].x() - adjustedMinTime) * (width() - 2 * margin) / (adjustedMaxTime - adjustedMinTime);
        int y1 = height() - margin - (data[i - 1].y() - minValue) * (height() - 2 * margin) / (maxValue - minValue);
        int x2 = margin + (data[i].x() - adjustedMinTime) * (width() - 2 * margin) / (adjustedMaxTime - adjustedMinTime);
        int y2 = height() - margin - (data[i].y() - minValue) * (height() - 2 * margin) / (maxValue - minValue);

        painter->drawLine(x1, y1, x2, y2);
    }
}
